<template>
	<div class="front-padding" v-loading="loading" element-loading-text="数据加载中..." element-loading-spinner="el-icon-loading" element-loading-background="#fff">
		<el-input v-model="searchText" placeholder="输入关键字" style="margin: 0 0 10px!important"></el-input>
		<div class="tree bj bg-white">
			<el-scrollbar style="height: 100%;">
				<el-tree :data="treeData" accordion node-key="id" :props="defaultProps" :filter-node-method="filterNode" ref="tree" @node-click="nodeClick" :default-expand-all="true" highlight-current :expand-on-click-node="false"></el-tree>
			</el-scrollbar>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			loading: true,
			searchText: '',
			treeData: [],
			defaultProps: {
                label: 'name',
                children: 'subAssertList'
            }
		}
	},
	watch: {
        searchText(val) {
            this.$refs.tree.filter(val);
        }
    },
    computed: {
    	systemId() {
    		return this.$store.state.systemId;
    	}
    },
    methods: {
    	getTreeData() {         //获取树形结构数据
    		this.loading = true;
            this.$http.get('subjectGroup/selectUnion?systemId=' + this.systemId).then(res => {
                if(res.data.code == 20001) {
                	setTimeout(() => {
                		this.loading = false;
                		if(res.data.data) {
	                        this.treeData = res.data.data;
	                        this.setTreeDataNo(this.treeData);
	                    }
                	}, 250);
                } else {
                	setTimeout(() => {
                		this.loading = false;
                	}, 250);
                }
            }).catch(() => {
            	setTimeout(() => {
            		this.loading = false;
            	}, 250);
            });
        },
        filterNode(value, data) {       //过滤查询树形节点
            if (!value) return true;
            return data.name.indexOf(value) !== -1;
        },
        setTreeDataNo(arr) {     //递归设置（会计科目）树形结构节点加上编码
            for(let key in arr) {
                if(arr[key]['name'] !== '资产' && arr[key]['name'] !== '负债' && arr[key]['name'] !== '共同' && arr[key]['name'] !== '权益' && (arr[key]['name'] !== '成本' || arr[key]['code'] == '10130') && arr[key]['name'] !== '损益' && arr[key]['name'] !== '表外') {
                    arr[key]['name'] = arr[key]['code'] + ' (' + arr[key]['name'] + ')';
                }
                if(arr[key]['subAssertList'] && arr[key]['subAssertList'].length > 0) {
                    this.setTreeDataNo(arr[key]['subAssertList']);
                }
            }
        },
        nodeClick(row) {		//树形结构节点触发
            let store = this.$store.state.verifyProcessStore;
            let data = {
                id: row['id'],
                name: row['name'],
                level: 1,
                billId: store.billId,
                pid: store.billId,
                children: [],
                subList: [
                    {
                        id: store.dfId,
                        name: store.dfName,
                        level: 2,
                        parentId: 0,
                        parentName: '无',
                        billId: store.billId,
                        pid: row['id'],
                        children: [],
                        subList: [
                            {
                                id: store.dfId,
                                name: store.dfName,
                                level: 3,
                                billId: store.billId,
                                vercharId: row['id'],
                                pid: store.dfId,
                                children: [],
                                subList: [
                                    {
                                        id: store.dfId,
                                        name: store.minMoney + ' ~ ' + store.maxMoney,
                                        level: 4,
                                        money1: store.minMoney,
                                        money2: store.maxMoney,
                                        billId: store.billId,
                                        vercharId: row['id'],
                                        deptId: store.dfId,
                                        pid: store.dfId,
                                        children: [],
                                        subList: []
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
            this.$store.commit('verifyProcessStore/appendNode', data);
            this.close();
        },
        close() {		//关闭
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    },
    created() {
    	this.getTreeData();
    }
}
</script>
<style scoped>
	.tree {height: calc(100% - 50px);box-sizing: border-box;padding: 5px 0;}
	.tree .el-tree {height: 100% !important;overflow-y: auto !important;}
</style>